--[[
	Project:		TruRP
	Author:			Martin 'Ploo' Pucilowski
	File:			cl_vgui.lua
	Date:			02/2010
	Description:	Handles all of VGUI
--]]

function ShowPlayerMenu( ply, tab )
	--if Menu then Menu:SetVisible( true ) return end
	Menu = vgui.Create("DFrame")
	Menu:SetSize(640, 480)
	Menu:Center()
	Menu:SetVisible( true )
	Menu:MakePopup( )
	Menu:SetTitle("TruRP Player Menu")
	
	Menu.Tabs = vgui.Create( "DPropertySheet", Menu )
	Menu.Tabs:StretchToParent(4, 28, 4, 4)
	
	Menu.TabData = {
		{ 
			Name = "Character",
			Panel = Character(),
			Icon = "gui/silkicons/user",
			Description = "Information about you!"
		},
		{ 
			Name = "Finances",
			Panel = Finances(),
			Icon = "gui/silkicons/money",
			Description = "Your finances."
		},
		{ 
			Name = "Inventory",
			Panel = Inventory(),
			Icon = "gui/silkicons/box",
			Description = "What's in your backpack."
		},
		{ 
			Name = "Occupation",
			Panel = Occupation(),
			Icon = "gui/silkicons/group",
			Description = "Get a job!"
		},
		{ 
			Name = "Appearance",
			Panel = Appearance(),
			Icon = "gui/silkicons/palette",
			Description	= "Time for a makeover?"
		},
		{ 
			Name = "Perks",
			Panel = Perks(),
			Icon = "gui/silkicons/star",
			Description = "Have an advantage!"
		},
		{ 
			Name = "Settings",
			Panel = Settings(),
			Icon = "gui/silkicons/wrench",
			Description = "Change settings."
		},
		{ 
			Name = "Help",
			Panel = Help(),
			Icon = "gui/silkicons/magnifier",
			Description = "What the fuck do I do?"
		}
	}
	
	for I = 1, #Menu.TabData do
		Sheet = Menu.TabData[I]
		if Sheet.Description then
			Menu.Tabs:AddSheet(Sheet.Name, Sheet.Panel, Sheet.Icon or nil, false, false, Sheet.Description)
		else
			Menu.Tabs:AddSheet(Sheet.Name, Sheet.Panel, Sheet.Icon or nil, false, false)
		end
	end

	Menu.Tabs.SetActiveTab( tab )
	
	--function Menu:Close()
		--Menu:SetVisible(false)
	--end	
end
concommand.Add("RP_PlayerMenu", function( ply, cmd, args )
	ShowPlayerMenu( ply, args[1] or 1 )
end )



	function Character()
		Tab = vgui.Create( "DPanel", Menu.Tabs );
		return Tab
	end
	function Finances()
		Tab = vgui.Create( "DPanel", Menu.Tabs );
		return Tab
	end
	function Occupation()
		Tab = vgui.Create( "DPanel", Menu.Tabs );
		return Tab
	end
	function Inventory()
		Tab = vgui.Create( "DPanel", Menu.Tabs )
		return Tab
	end
	function Appearance()
		Tab = vgui.Create( "DPanel", Menu.Tabs )
		return Tab
	end
	function Perks()
		Tab = vgui.Create( "DPanel", Menu.Tabs )
		return Tab
	end
	function Settings()
		Tab = vgui.Create( "DPanel", Menu.Tabs );
		return Tab
	end





local function ShowItemMenu()
	HiddenButton = vgui.Create("DButton")
	HiddenButton:SetSize(ScrW(), ScrH())
	HiddenButton:SetText("")
	HiddenButton:SetDrawBackground(false)
	HiddenButton:SetDrawBorder(false)
	HiddenButton.DoClick = function()
		local pos = gui.ScreenToVector(gui.MouseX(), gui.MouseY())
		local tracedata = {}
		tracedata.start = LocalPlayer():GetShootPos()
		tracedata.endpos = LocalPlayer():GetShootPos() + (pos * 96)
		tracedata.filter = LocalPlayer()
		local tr = util.TraceLine(tracedata)
		local item = tr.Entity

		if not ValidWorldItem( item ) then return end
		
		local ItemMenu = DermaMenu()
		ItemMenu:AddOption("Eat", function() end )
		ItemMenu:AddOption("Take", function() end )
		ItemMenu:AddOption("Cancel", function() end )
		ItemMenu:Open()	
	end
end
ShowItemMenu()






























function DrawPropertyMenu()
	local ply = LocalPlayer()
	local door = ply:TraceDoor()
	if not door then return end
	
	local Title = ""
	if door:GetNWString("PropertyTitle") != "" then
		Title = door:GetNWString("Title")
	else Title = nil end
	
	local DoorMenu = vgui.Create("DFrame")
	DoorMenu:SetSize(160, 171)
	DoorMenu:Center()
	DoorMenu:SetVisible( true )
	DoorMenu:MakePopup( )
	DoorMenu:SetTitle(Title or "Door")

	
	DoorMenu.SetdoorTitle = vgui.Create( "DButton", DoorMenu )
	DoorMenu.SetdoorTitle:SetSize( 152, 32 )
	DoorMenu.SetdoorTitle:SetPos( 4, 27 )
	DoorMenu.SetdoorTitle:SetText( "Set door name" )
	DoorMenu.SetdoorTitle.DoClick = function(  )
		Derma_StringRequest("Set door title", "Set the title of the door you're looking at", Title or "", 
		function( text ) RunConsoleCommand("RP_doorSetTitle", text) DoorMenu:Close() end, function() end, "Confirm", "Cancel")
	end
	
	DoorMenu.Selldoor = vgui.Create( "DButton", DoorMenu )
	DoorMenu.Selldoor:SetSize( 152, 32 )
	DoorMenu.Selldoor:SetPos( 4, 27 + 36 )
	DoorMenu.Selldoor:SetText( "Sell door" )
	DoorMenu.Selldoor.DoClick = function(  )
		RunConsoleCommand("RP_doorSell")
		DoorMenu:Close()
	end
	
	DoorMenu.Selldoor = vgui.Create( "DButton", DoorMenu )
	DoorMenu.Selldoor:SetSize( 152, 32 )
	DoorMenu.Selldoor:SetPos( 4, 27 + 36 + 36 )
	DoorMenu.Selldoor:SetText( "Add Co-owner" )
	DoorMenu.Selldoor.DoClick = function(  )
		local Menu = DermaMenu()
		for _, v in pairs(player.GetAll()) do
			if not door:IsOwner(v) and not door:IsCoOwner(v) and not door:CanOwn(v) and not door:CanCoOwn(v) then
				Menu:AddOption(v.Name, function()
					RunConsoleCommand("RP_DoorAddCoOwner", v:UserID())
					DoorMenu:Close()
				end )
			end
		end
		Menu:AddOption("Cancel", function() end )
		Menu:Open()
	end
	
	DoorMenu.Selldoor = vgui.Create( "DButton", DoorMenu )
	DoorMenu.Selldoor:SetSize( 152, 32 )
	DoorMenu.Selldoor:SetPos( 4, 27 + 36 + 36 + 36 )
	DoorMenu.Selldoor:SetText( "Remove Co-owner" )
	DoorMenu.Selldoor.DoClick = function(  )
		local Menu = DermaMenu()
		for _, v in pairs(player.GetAll()) do
			if door:IsCoOwner(v) or door:CanCoOwn(v) then
				Menu:AddOption(v.Name, function()
					RunConsoleCommand("RP_doorRemoveCoOwner", v:UserID())
					DoorMenu:Close()
				end )
			end
		end
		Menu:AddOption("Cancel", function() end )
		Menu:Open()
	end
end
concommand.Add("RP_PropertyMenu", DrawPropertyMenu)